<route lang="json5">
  {
    style: {
      navigationBarTitleText: '',
      navigationBarBackgroundColor: '#FAD0C4',
      navigationBarTextStyle: 'black',
    },
  }
  </route>
<template>
  <view class="container">
    <!-- 用户信息区域 -->
    <view v-if="userStore.userInfo.token" class="user-info">
      <view class="avatar-wrapper">
        <image class="avatar" :src="userStore.userInfo.avatar" />
      </view>
      <view class="user-detail">
        <view class="name-row">
          <text class="nickname">{{ userStore.userInfo.nickname }}</text>
          <view class="stars">

            <uni-icons v-if="userStore.userInfo.group_id == 3" v-for="i in 2" :key="i" type="star-filled" size="16"
              color="#FFB800"></uni-icons>
            <uni-icons v-if="userStore.userInfo.group_id == 2" type="star-filled" size="16" color="#FFB800"></uni-icons>
          </view>
        </view>
        <text class="user-id">ID: {{ userStore.userInfo.id }}</text>
      </view>

    </view>
    <view v-else class="flex flex-col items-center justify-center py-4" @click="navTo('/pages/login/login')">
      <view class="w-24 h-24 rounded-full overflow-hidden mb-2 flex items-center justify-center bg-white">
        <!-- <view class="iconfont fa-solid fa-user text-orange-500 text-4xl"></view> -->
        <wd-icon name="user" size="60px" color="#cccccc" />
      </view>
      <view class="text-gray-500 text-lg font-bold">未登录，点击登录</view>
      <view class="text-gray-500 text-sm">请登录以查看您的信息</view>
    </view>

    <!-- 数据统计区域 -->
    <view class="content-card">
      <view class="stats-container">
        <view class="stat-item" @click="handleOrderClick('/pages/my/goodsvalue')">
          <text class="stat-num">{{ indexData.cargo_value ? indexData.cargo_value : 0 }}</text>
          <text class="stat-label">宝豆</text>
        </view>
        <view class="stat-item" @click="handleOrderClick('/pages/my/score')">
          <text class="stat-num">{{ indexData.points ? indexData.points : 0 }}</text>
          <text class="stat-label">积分</text>
        </view>
        <!-- <view class="stat-item">
          <text class="stat-num">{{ indexData.points_price ? indexData.points_price : 0 }}</text>
          <text class="stat-label">积分价格</text>
        </view> -->
        <view class="stat-item" @click="handleOrderClick('/pages/my/wallet')">
          <text class="stat-num">{{ indexData.activity_points ? indexData.activity_points : 0 }}</text>
          <text class="stat-label">绿色积分</text>
        </view>
      </view>
    </view>


    <!-- 订单区域 -->
    <view class="content-card">
      <view class="section-container">
        <view class="section-title">我的订单</view>
        <view class="order-grid">
          <view class="order-item" @click="handleOrderClick('/pages/my/order?status=1')">
            <uni-icons type="shop" size="24" color="#333333"></uni-icons>
            <text class="order-label">待确认</text>
          </view>
          <view class="order-item" @click="handleOrderClick('/pages/my/order?status=2')">
            <uni-icons type="paperplane" size="24" color="#333333"></uni-icons>
            <text class="order-label">待发货</text>
          </view>
          <view class="order-item" @click="handleOrderClick('/pages/my/order?status=3')">
            <uni-icons type="checkbox-filled" size="24" color="#333333"></uni-icons>
            <text class="order-label">已完成</text>
          </view>
          <view class="order-item" @click="handleOrderClick('/pages/my/order')">
            <uni-icons type="list" size="24" color="#333333"></uni-icons>
            <text class="order-label">全部</text>
          </view>
        </view>
      </view>

    </view>


    <!-- 常用工具 -->
    <view class="content-card">
      <view class="section-container">
        <view class="section-title">常用工具</view>
        <view class="tools-grid">
          <view class="tool-item" v-for="(tool, index) in tools" :key="index" @click="handleToolClick(tool.url)">
            <uni-icons :type="tool.icon" size="24" color="#333333"></uni-icons>
            <text class="tool-label">{{ tool.name }}</text>
          </view>
          <view class="tool-item" v-if="userStore.userInfo.group_id == 4" v-for="(tool, index) in adminTools"
            :key="index" @click="handleToolClick(tool.url)">
            <uni-icons :type="tool.icon" size="24" color="#333333"></uni-icons>
            <text class="tool-label">{{ tool.name }}</text>
          </view>
        </view>
      </view>

    </view>



  </view>
</template>

<script lang="ts" setup>
import { navTo } from '@/utils'

import { useUserStore } from '@/store'
import { getUserIndexAPI, postUserDetailAPI } from '@/service/index/index'
import { onMounted, ref } from 'vue'
const userStore = useUserStore()
const tools = ref([
  { name: '积分记录', icon: 'medal', url: '/pages/my/score' },
  { name: '我的团队', icon: 'staff', url: '/pages/my/team' },
  { name: '转账', icon: 'upload', url: '/pages/my/score_transfer' },
  { name: '地址管理', icon: 'location', url: '/pages/my/address' },
  { name: '设置', icon: 'gear', url: '/pages/my/setting' },
  // { name: '宝豆', icon: 'shop', url: '/pages/my/goodsvalue' },


])

const adminTools = ref([
  { name: '充值', icon: 'plusempty', url: '/pages/my/recharge' },
  { name: '释放积分', icon: 'medal', url: '/pages/my/release_points' },
  // { name: '平台积分', icon: 'medal', url: '/pages/my/platform_score' },
  // { name: '购买限制', icon: 'locked', url: '/pages/my/buy_limit' },
  // { name: '宝豆减扣', icon: 'minus', url: '/pages/my/goodsvalue_reduce' }
])
const goLogin = () => {
  uni.navigateTo({
    url: '/pages/login/login',
  })
}

const handleOrderClick = (url: string) => {
  if (!userStore.userInfo.token) {
    uni.navigateTo({
      url: '/pages/login/login'
    })
    return
  }
  uni.navigateTo({
    url: url,
  })
}

const handleToolClick = (url: string) => {
  if (!userStore.userInfo.token) {
    uni.navigateTo({
      url: '/pages/login/login'
    })
    return
  }
  uni.navigateTo({
    url: url,
  })
}

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()

type UserInfo = {
  code: number
  data: {
    userinfo: any
  }
}
const indexData = ref({
  cargo_value: 0,
  points: 0,
  points_price: 0,
  activity_points: 0
})
const userInfo = ref<UserInfo | null>(null)
const getUserInfo = () => {
  postUserDetailAPI({

  }).then((res) => {
    if (res.code === 1) {
      indexData.value = res.data.userinfo
    }
  })
}

onShow(() => {
  if (userStore.userInfo.token) {
    getUserInfo()
  } else {
    // navTo('/pages/login/login')
  }
})

// #ifdef MP-WEIXIN
onLoad(options => {
  console.log(userStore.userInfo)
  if (userStore.userInfo.token) {
    getUserInfo()
  } else {
    // navTo('/pages/login/login')
  }
})

// #endif
// #ifdef H5
onMounted(() => {
  if (userStore.userInfo.token) {
    getUserInfo()
  } else {
    // navTo('/pages/login/login')
  }
})

// #endif

</script>

<style scoped>
page {
  height: 100%;
}

.container {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  background: linear-gradient(to bottom, #FAD0C4 15%, #f5f5f5 40%, #f5f5f5);
}

.user-info {
  display: flex;
  align-items: center;
  padding: 40rpx 32rpx;
}

.avatar-wrapper {
  width: 120rpx;
  height: 120rpx;
  margin-right: 24rpx;
  overflow: hidden;
  border-radius: 60rpx;
}

.avatar {
  width: 100%;
  height: 100%;
}

.user-detail {
  flex: 1;
}

.name-row {
  display: flex;
  align-items: center;
  margin-bottom: 8rpx;
}

.nickname {
  margin-right: 16rpx;
  font-size: 32rpx;
  font-weight: 500;
  color: #333333;
}

.stars {
  display: flex;
  align-items: center;
}

.user-id {
  font-size: 24rpx;
  color: #333333;
}

.content-card {
  padding: 0 20rpx;
}

.stats-container {
  display: flex;
  justify-content: space-between;
  padding: 32rpx;
  margin-bottom: 20rpx;
  background-color: #ffffff;
  border-radius: 20rpx;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-num {
  margin-bottom: 8rpx;
  font-size: 32rpx;
  font-weight: 500;
  color: #333333;
}

.stat-label {
  font-size: 24rpx;
  color: #666666;
}

.section-container {
  padding: 32rpx;
  margin-bottom: 20rpx;
  background-color: #ffffff;
  border-radius: 20rpx;
}

.section-title {
  margin-bottom: 24rpx;
  font-size: 28rpx;
  font-weight: 500;
  color: #333333;
}

.order-grid {
  display: flex;
  justify-content: space-between;
}

.order-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.order-label {
  margin-top: 12rpx;
  font-size: 24rpx;
  color: #666666;
}

.tools-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 32rpx;
}

.tool-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tool-label {
  margin-top: 12rpx;
  font-size: 24rpx;
  color: #666666;
}

.tab-bar {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100rpx;
  background-color: #ffffff;
  border-top: 1px solid #f0f0f0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tab-label {
  margin-top: 4rpx;
  font-size: 20rpx;
  color: #666666;
}

.tab-label.active {
  color: #1890ff;
}
</style>
